﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Matrix.Xmpp.Client;
using Matrix.Xmpp.Roster;
using Matrix;

namespace ChatBox
{
    public partial class Login : System.Web.UI.Page
    {
        protected XmppClient xmppClient = new XmppClient();
        private List<RosterItem> contacts = new List<RosterItem>();

        protected void Page_Load(object sender, System.EventArgs e)
        {
            LBContacts.Items.Clear();
            LBContacts.Attributes.Add("onchange", "SetLabelToCurrentRecepiant()");

            if(Session["users"]!=null)
            {
                List<RosterItem> items = (List<RosterItem>)Session["users"];
                foreach(RosterItem contact in items)
                {
                    if(Convert.ToBoolean(contact.GetAttribute("Status")))
                    { 
                        if (contact.Name != null)
                        {
                            LBContacts.Items.Add(contact.Name + contact.GetAttribute("Show").ToString());
                        }
                        else
                        {
                            LBContacts.Items.Add(contact.Jid + contact.GetAttribute("Show").ToString());
                        }
                    }        
                }
            }
        }

      

        void xmppClient_OnRosterEnd(object sender, Matrix.EventArgs e)
        {
            if(Session["users"] != null)
            { 
                List<RosterItem> oldItems = ((List<RosterItem>) Session["users"]);
                for (int i = 0; i < oldItems.Count; i++)
                {
                    for (int j = 0; j < contacts.Count; j++)
                    {
                        if(oldItems[i].Jid.Bare == contacts[j].Jid.Bare)
                        {
                            contacts[j] = oldItems[i];
                        }
                    }
                }
            }
             Session["users"] = contacts;
        }

        void xmppClient_OnRosterItem(object sender, RosterEventArgs e)
        {
            contacts.Add(e.RosterItem);
        }

        void xmppClient_OnRosterStart(object sender, Matrix.EventArgs e)
        {
            contacts = new List<RosterItem>();
        }

        private void RefreshPage()
        {
            XmppClient client = (XmppClient)Session["xmppClient"];
            client.RequestRoster();
            client.OnPresence += xmppClient_OnPresence;
        }

        protected void LoginButton_Click(object sender, System.EventArgs e)
        {
            xmppClient.OnRosterStart += xmppClient_OnRosterStart;
            xmppClient.OnRosterItem += xmppClient_OnRosterItem;
            xmppClient.OnRosterEnd += xmppClient_OnRosterEnd;
            xmppClient.OnPresence += xmppClient_OnPresence;
            xmppClient.SetUsername(TbNaam.Text);
            xmppClient.Password = tbPassword.Text;
            xmppClient.SetXmppDomain("gmail.com");
            xmppClient.AutoRoster = true;
            xmppClient.AutoPresence = true;
            xmppClient.Status = "Available";
            xmppClient.Show = Matrix.Xmpp.Show.chat;
            xmppClient.Open();
            Session["xmppClient"] = xmppClient; 
        }

        void xmppClient_OnPresence(object sender, PresenceEventArgs e)
        {
            List<RosterItem> items = (List<RosterItem>)Session["users"];
            foreach(RosterItem item in items)
            {

                if(item.Jid.User == e.Presence.From.User)
                {
                    string status;
                    switch(e.Presence.Show.ToString())
                    {
                        
                        case "NONE": status = " (Online)"; break;
                        case "away": status = " (AFK)"; break;
                        case "chat": status = " (In Chat)"; break;
                        case "dnd": status = " (Bezet)"; break;
                        default: status=e.Presence.Show.ToString(); break;

                    }
                    item.SetAttribute("Status", ((e.Presence.Type.ToString() == "available") ? true : false));
                    item.SetAttribute("Show", status);
                }
            }
            Session["users"] = items;
        }

      
        private void CloseSession()
        {
            xmppClient.SendUnavailablePresence("Logged Out");
            xmppClient.Close();
            Session.Remove("xmppClient");
        }

        protected void LogoutButton_Click(object sender, System.EventArgs e)
        {
            CloseSession();
        }

        protected void RefreshButton_Click(object sender, System.EventArgs e)
        {
            RefreshPage();
        }
    }
}